"""
@author: zjc
@file: zuoye.py
@time: 2022/11/4 17:38
"""
# import random
# s_person={"A":"a","B":"b","C":'c',"D":'d'}
# g_person={"A":"","B":"","C":'',"D":''}
# while  ""  in  g_person.values():
#     for key,value in s_person.items():
#         r_person=random.choice(list(g_person))
#         if key != r_person :
#            g_person[r_person]=value
#         if list(g_person.values()).count(value)>1:
#             g_person[r_person]=''
# print(g_person)


# 随机点名系统
# person=["A","B","C","D",'E']
# l_person=input("请输入要请假的学生:").split(" ")
# if l_person == ['']:
#     print("今天没有人请假")
# while True:
#     r_person = random.choice(person)
#     if r_person in l_person:
#         print(f"{r_person}同学今天请假,请重新抽取")
#     else:
#         print(f"请{r_person}同学回答问题")
#         break
# l_person.clear()

# 三乐购物系统
import json
print("------欢迎来到三乐购物系统!------")

#客人信息
message = {}

# 注册
def regist():
    with open("客人信息.txt",'a',encoding="utf-8") as fw:
        root = input("请输入用户名:")
        passwd = input("请输入密码:")
        money = input("请输入金额:")
        message[root]={"passwd":passwd,"money":money}
        js = json.dumps(message,ensure_ascii=False)
        fw.write(js+"\n")


# 登录
def login():
    fr=open("客人信息.txt",'r',encoding="utf-8")
    root = input("请输入用户名:")
    passwd = input("请输入密码:")
    lines = fr.readlines()
    message_l={}
    for line in lines:
        message_c=eval(line)
        message_l=dict(message_l,**message_c)

    if root in message_l and passwd == message_l[root]['passwd']:
        print("登陆成功")
    elif root in message_l and passwd != message_l[root]["passwd"]:
        print("密码错误,请重新输入密码")
    else:
        print("没有该用户，请注册")

#商品列表
goods={"原神胡桃手机":4200,"平板电脑":6000,"崩坏三八重樱手办":998,"fgo自抽号":30,
           "一箱橘子":58,"阿拉哈托人偶":600}

#展示商品
def shopping(dict):
    for key,value in dict.items():
        print(key,"-->",value)

#购物车
buy = {}

#购买商品
def shopping_car(key):
    with open("购物车.txt",'a+',encoding="utf-8") as fw:
        buy[key]=goods[key]
        js = json.dumps(buy, ensure_ascii=False)
        fw.write(js)
        fw.write("\n")

#取消购买
def del_shopping_car(key):
    fr = open("购物车.txt", 'r', encoding="utf-8")
    lines = fr.readlines()
    buy2={}
    for line in lines:
        buy1 = eval(line)
        buy2 = dict(buy2, **buy1)
    fr.close()
    del buy2[key]

#查看购物车
def myshopping_car():
    fr = open("购物车.txt", 'r', encoding="utf-8")
    lines = fr.readlines()
    buy2 = {}
    for line in lines:
        buy1= eval(line)
        buy2=dict(buy2,**buy1)
    fr.close()
    for key,value in buy2.items():
        print(key,"-->",value)


#支付
def pay():
    sum = 0
    with open("购物车.txt", 'r+', encoding="utf-8") as fr:
        lines = fr.readlines()
        buy2 = {}
        for line in lines:
            buy1 = eval(line)
            buy2 = dict(buy2, **buy1)
        for key, value in buy2.items():
            sum += value
        print("商品总共要:", sum, "元")
        fr.truncate(0)




while True:
    print("您可以进行的操作:\n","1.登录\n","2.注册\n","3.退出三创商城")
    num = input("请输入您要进行的操作:")
    if num == '1':
        login()
        print("-----商品信息:")
        shopping(goods)
        while True:
            print("您可以进行的操作:\n","1.商品购买\n", "2.取消购买\n","3.查看购物车\n",
                  "4.支付账单\n","5.退出购物系统")
            num1 = input("请输入您要进行的操作:")
            if num1 == '1':
                goods1 = input("请输入要购买的商品:")
                shopping_car(goods1)
            elif num1 == '2':
                goods2 = input("请删除不想购买的商品:")
                del_shopping_car(goods2)
            elif num1 == '3':
                myshopping_car()
            elif num1 == '4':
                pay()
            elif num1=='5':
                break
            else:
                print("您输入有误，请重新输入")
    elif num == '2':
        regist()
    elif num == '3':
        print("欢迎下次光临!")
        break
    else:
        print('您输入的操作有误,请重新输入你要进行的操作')











